<?php
namespace Admin\Controller;
use Think\Crypt;
use Think\Verify;
class LoginController extends BaseController {
    
   function index(){
       if(IS_POST){
           $vcode=I('post.vcode');
           $mycode=new Verify();
           $username=I('post.username');
           $password=I('post.password');
         if($mycode->check($vcode)){           
               $rember=I('post.remember',0);
               $users_tb=D('users');
               $row=$users_tb->where(array('username'=>$username,'status'=>0))->find();
               //print_r($row);die;
          if($row){
             $pwd=$users_tb->md5($password,$row['salt']);
             //print_r($pwd);die;
             if($pwd==$row['password']){
                 //账号 密码是正确的
                 session('username',$username);//保存用户名到session
                 session('gid',$row['gid']);
                 $data=array(
                     'lastlogin'=>time(),
                     'ip'=>get_client_ip()
                     //获取IP地址
                 );                 
                 $users_tb->where(array('username'=>$username))->save($data);
                 
                 if($rember){
                        $data=array(
                            'u'=>Crypt::encrypt($username, 'username'),
                            'p'=>Crypt::encrypt($pwd, 'password'),
                        );
                     cookie('_token_',$data,3600);
                 }
                 $this->redirect('index/index');
             }
       }
       
                $this->assign('err','账号或者密码错误');
           }else{
                $this->assign('err','验证码错误');
                $this->assign(array('username'=>$username,'password'=>$password));
           }
       }
       $this->display(); 
   }
   
   function logout(){
       
       session(null);
       cookie('_token_',null);
       //die;
       //dump($_SESSION);
       //dump($_COOKIE);die;
       $this->redirect('index');
       
   } 
   
   function test(){
       
      $reslut= crypt::encrypt('admin','aacf99');
      
      
      
      echo $reslut;
   }
    /* function cookie(){
        $info=array('age'=>20,'sex'=>1);
        cookie('username','admin',3600);
        cookie('info','$info',8*60*60);
        echo cookie('username');
        //cookie('username',null);
        //cookie(null);
        
        print_r(cookie('info'));
       //  print_r($_COOKIE);
       
        var_dump(cookie('?username'));
    } */
   function getCode(){
       $config=array(
           'fontSize'=>30,
           'length'=>4,
           'useNoise'=>false,//开启关闭杂点
       );
       $mycode=new Verify($config);//实例化参数
       $mycode->entry();
   }
}